<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目业态" prop="regionId">
        <el-select v-model="queryParams.regionId" placeholder="请选择">
          <el-option v-for="dict in dict.type.cld_project_business" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['cld:regionMaterial:add']">项目业态物资清单设置</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialList" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目业态" align="center" prop="regionName" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['cld:regionMaterial:edit']">修改物资清单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改业态物资清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="2000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目业态" prop="regionId">
          <el-select v-model="form.regionId" placeholder="请选择">
            <el-option v-for="dict in dict.type.cld_project_business" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button icon="el-icon-plus" plain size="mini" type="primary" @click="handleAddCommodity">添加商品</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-table :data="form.detailsList" border>
            <el-table-column align="center" label="商品类别" prop="commodityType"/>
            <el-table-column align="center" label="商品编号" prop="commodityNo"/>
            <el-table-column align="center" label="商品名称" prop="commodityName1"/>
            <el-table-column align="center" label="商品规格" prop="commoditySpec"/>
            <el-table-column align="center" label="商品单位" prop="unitName1"/>
            <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
              <template slot-scope="scope">
                <el-button icon="el-icon-delete" size="mini" type="text" @click="handleDeleteCommodity(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择商品 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isCommodityOpen" append-to-body title="选择商品" width="80%">
      <ErpCommodity v-if="isCommodityOpen" :notCommodityIds="notCommodityIds" :regionId="form.regionId" @SelectionChange="handleSelectionChange"></ErpCommodity>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedCommodity">确 定</el-button>
        <el-button @click="isCommodityOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { listRegionMaterial, getRegionMaterial, delRegionMaterial, addRegionMaterial, updateRegionMaterial } from "@/api/cld/regionMaterial";
import ErpCommodity from "@/views/erp/ErpCommodity/selectedCommodity.vue";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "RegionMaterial",
  components: {ErpCommodity,Treeselect},
  dicts: ['cld_project_business'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业态物资清单表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regionId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        regionId: [
          {required: true, message: "请选择项目业态", trigger: "blur"}
        ],
      },
      // 选择商品页面
      isCommodityOpen: false,
      // 项目业态物资清单
      regionMateial: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询业态物资清单列表 */
    getList() {
      this.loading = true;
      listRegionMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        regionId: null,
        detailsList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目业态物资清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const regionMaterialId = row.regionMaterialId || this.ids
      getRegionMaterial(regionMaterialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目业态物资清单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.regionMaterialId != null) {
            updateRegionMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRegionMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const regionMaterialIds = row.regionMaterialId || this.ids;
      this.$modal.confirm('是否确认删除业态物资清单编号为"' + regionMaterialIds + '"的数据项？').then(function() {
        return delRegionMaterial(regionMaterialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cld/regionMaterial/export', {
        ...this.queryParams
      }, `regionMaterial_${new Date().getTime()}.xlsx`)
    },
    // 选择商品
    handleAddCommodity() {
      // 设置商品过滤id
      this.notCommodityIds = this.form.detailsList?.map(item => item.commodityId);
      this.isCommodityOpen = true;
    },
    // 商品选择
    handleSelectionChange(selection) {
      this.temporyList = selection;
    },
    // 确定选择商品
    submitSelectedCommodity() {
      this.temporyList.forEach(item => {
        this.form.detailsList.push(item)
      })
      this.isCommodityOpen = false;
    },
    /** 删除商品 */
    handleDeleteCommodity(row) {
      const index = this.form.detailsList.indexOf(row);
      if (index > -1) {
        this.form.detailsList.splice(index, 1);
      }
    },
  }
}
</script>
<style lang="css" scoped>
::v-deep .el-table__cell{
  padding: 0px;
}
</style>
